﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models;
using Utilities;
using Telerik.Web.Mvc;

namespace Grant_Warehouse.Controllers
{ 
    public class CategoryController : Controller
    {
        public ViewResult Index()
        {
            return View(CH.GetAllData<Category>());
        }

        [GridAction]
        public ActionResult AjaxCategoryIndex()
        {
            return View(CT.GetViewModel<Category>());
        }

        public ViewResult Details(int id)
        {
            return View(CH.GetDataById<Category>(id));
        }

        public ActionResult Create()
        {
            return View();
        } 

        [HttpPost]
        public ActionResult Create(Category category)
        {
            CH.Create<Category>(category);
            return RedirectToAction("Index");
        }
 
        public ActionResult Edit(int id)
        {
            return View(CH.GetDataById<Category>(id));
        }

        [HttpPost]
        public ActionResult Edit(Category category)
        {
            CH.Edit<Category>(category);
           return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            return View(CH.GetDataById<Category>(id));
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                CH.Delete<Category>(id);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "此数据被其他数据引用，为保证数据完整性，请先移除对此数据的引用，再进行删除");
                Log.LogError(ex,this);
                return View(CH.GetDataById<Category>(id));
            }
            return RedirectToAction("Index");
        }
    }
}